package com.luocj.demo;

import android.Manifest;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.mob.tools.utils.UIHandler;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MainActivity extends AppCompatActivity implements Handler.Callback {

    //   133d9390ae7a8911257b71e8db8b2b00
//   wx1ae587b42b65590f
//    sharesdk
//    Appkey : 244d3ebdb8c4a | App Secret : 1ec667a1dbd79ff4f891232c338e6ca3
    private String SHARE_APP_KEY = "244d3ebdb8c4a";
    private String SHARE_APP_SECRET = "1ec667a1dbd79ff4f891232c338e6ca3";

    private String WX_APP_ID = "wx1ae587b42b65590f";
    private String WX_APP_SECRET = "133d9390ae7a8911257b71e8db8b2b00";

    private String QQ_APP_ID = "1106493494";
    private String QQ_APP_KEY = "95DoBmxhypXGKkwi";

    private String TAG = MainActivity.class.getSimpleName();
    private int MSG_USERID_FOUND = 1001;

    private int QZONE_SHARE_CANCEL = 2001;
    private int QZONE_SHARE_COMPLETE = 2002;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Observer<Permission>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Permission permission) {
                        if (permission.granted) {
                            Log.i(TAG, "onNext: " + "1");
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            Log.i(TAG, "onNext: " + "should ask again");
                        } else {
                            Log.i(TAG, "onNext: " + "denie");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });


        findViewById(R.id.btn_wx_share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "onClick: " + "__");
                Platform.ShareParams shareParams = new Platform.ShareParams();
                shareParams.setShareType(Platform.SHARE_IMAGE);
                shareParams.setImageData(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
                shareParams.setTitle("title");
                shareParams.setText("text");
                shareParams.setUrl("www.baidu.com");
                Platform weChat = ShareSDK.getPlatform(Wechat.NAME);
                weChat.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        Log.i(TAG, "onComplete: " + hashMap.toString());
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        Log.i(TAG, "onError: " + throwable.toString());
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {

                    }
                });
                weChat.share(shareParams);

            }
        });

        findViewById(R.id.btn_wx_monent).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Platform.ShareParams shareParams = new Platform.ShareParams();
                shareParams.setShareType(Platform.SHARE_IMAGE);
                shareParams.setImageData(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
                shareParams.setTitle("title");
                shareParams.setText("text");
                shareParams.setUrl("www.baidu.com");
                Platform weChat = ShareSDK.getPlatform(WechatMoments.NAME);
                weChat.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        Log.i(TAG, "onComplete: " + hashMap.toString());
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        Log.i(TAG, "onError: " + throwable.toString());
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {

                    }
                });
                weChat.share(shareParams);

            }
        });

        //分享到QQzone
        findViewById(R.id.btn_qzone_share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Platform.ShareParams shareParams = new Platform.ShareParams();
                shareParams.setShareType(Platform.SHARE_IMAGE);
                shareParams.setImageData(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
                shareParams.setTitle("title");
                shareParams.setTitleUrl("http://q.qlogo.cn/qqapp/1106493494/BACC3584BD7623D1B3AA60CB07A7697E/100");
                shareParams.setText("text");
                shareParams.setUrl("http://q.qlogo.cn/qqapp/1106493494/BACC3584BD7623D1B3AA60CB07A7697E/100");
                Platform qzone = ShareSDK.getPlatform(QZone.NAME);
                qzone.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        Log.i(TAG, "onComplete: ----->" + platform.getDb().toString());
                        Message message = new Message();
                        message.obj = platform;
                        message.what = QZONE_SHARE_COMPLETE;
                        UIHandler.sendMessage(message, MainActivity.this);
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        Log.i(TAG, "onError: ----->" + throwable.toString());
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        Log.i(TAG, "onCancel: ----->" + platform.getDb());
                        Message message = new Message();
                        message.obj = platform;
                        message.what = QZONE_SHARE_CANCEL;
                        UIHandler.sendMessage(message, MainActivity.this);
                    }
                });
                qzone.share(shareParams);

            }
        });

        findViewById(R.id.btn_qq_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "onClick: " + "qqlogin");
                Platform platform = ShareSDK.getPlatform(QQ.NAME);
                platform.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        String userId = platform.getDb().getUserId();//获取用户账号
                        String userName = platform.getDb().getUserName();//获取用户名字
                        String userIcon = platform.getDb().getUserIcon();//获取用户头像
                        String userGender = platform.getDb().getUserGender(); //获取用户性别，m = 男, f = 女，如果微信没有设置性别,默认返回null

                        Log.i(TAG, "onComplete: "
                                + "userId------>" + userId + "\n"
                                + "userName------>" + userName + "\n"
                                + "userIcon------>" + userIcon + "\n"
                                + "userGender------>" + userGender);
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        Log.i(TAG, "onError: " + throwable.toString());
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        Log.i(TAG, "onCancel: " + i);
                    }
                });
                platform.SSOSetting(false);
                if (platform.isAuthValid()) { //如果授权就删除授权资料
                    platform.removeAccount(true);
                }
                platform.showUser(null);//授权并获取用户信息
//                authorize(platform);

            }
        });
    }

    //执行授权,获取用户信息
    private void authorize(Platform plat) {
        if (plat.isAuthValid()) { //如果授权就删除授权资料
            plat.removeAccount(true);
        }
        plat.showUser(null);//授权并获取用户信息
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 2001:
                Log.i(TAG, "handleMessage: "+msg.what);
                break;
            case 2002:
                Log.i(TAG, "handleMessage: "+msg.what);
                break;
        }
        return false;
    }
}
